﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Mmeller.Net.Decoder
{
    public class XmlHeader : FileHeader
    {
        private string rootName;
        public XmlHeader(string rootName)
        {
            this.rootName = rootName;
        }

        public override Dictionary<string, string> getFields(string filePath)
        {
            Dictionary<string, string> fields = new Dictionary<string, string>();

            XElement root = XElement.Load(filePath);

            foreach (XElement node in root.Elements(this.rootName))
            {
                fields[node.Name.ToString()] = node.Value;
                
            }

            return fields;
        }

        public override void createHeader(string filePath, List<HeaderField> fields)
        {
            XElement root = new XElement(this.rootName);

            foreach (HeaderField field in fields)
            {
                XElement node = new XElement(field.name);
                node.Value = field.value;
                root.Add(node);
            }

            root.Save(filePath);
        }
    }
}
